<?php

class User_Auth_Database
{

	/**
	 * authenticate database
	 * @param array $data
	 */
	public function login($data)
	{
		extract($data, EXTR_SKIP);

		if (!isset($username))
		{
			return FALSE;
		}

		if (!isset($password))
		{
			return FALSE;
		}

		/**
		 * fetch user by id.
		 */
		$table = Nuo::getTable('user');
		$select = $table -> select() -> where('email=?', (string)$username) -> orWhere('username=?', (string)$username);
		$user = $table -> fetchRow($select);

		/**
		 * identity is not match
		 */
		if (!$user)
		{
			return -1;
		}

		/**
		 * not validate account
		 */
		if (NULL == $user -> email)
		{
			return -2;
		}

		/**
		 * check password.
		 */
		if ($user -> isPassword($password))
		{
			return $user;
		}
	}

}
